home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / CBuilder / Setup / BCB / data.z / graphics.int < prev    next >
Encoding:
Text File  |  1998-02-09  |  32.0 KB  |  801 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       Delphi Visual Component Library                 }
  5. {                                                       }
  6. {       Copyright (c) 1995,98 Borland International     }
  7. {                                                       }
  8. {*******************************************************}
  9.  
  10. unit Graphics;
  11.  
  12. {$P+,S-,W-,R-}
  13. {$C PRELOAD}
  14.  
  15. interface
  16.  
  17. uses Windows, SysUtils, Classes;
  18.  
  19. { Graphics Objects }
  20.  
  21. type
  22.   TColor = $80000000..$7FFFFFFF;
  23.   {$NODEFINE TColor}
  24.  
  25.   (*$HPPEMIT 'namespace Graphics'*)
  26.   (*$HPPEMIT '{'*)
  27.   (*$HPPEMIT '  enum TColor {clMin=-0x7fffffff-1, clMax=0x7fffffff};'*)
  28.   (*$HPPEMIT '}'*)
  29.  
  30.  
  31. const
  32.   clScrollBar = TColor(COLOR_SCROLLBAR or $80000000);
  33.   clBackground = TColor(COLOR_BACKGROUND or $80000000);
  34.   clActiveCaption = TColor(COLOR_ACTIVECAPTION or $80000000);
  35.   clInactiveCaption = TColor(COLOR_INACTIVECAPTION or $80000000);
  36.   clMenu = TColor(COLOR_MENU or $80000000);
  37.   clWindow = TColor(COLOR_WINDOW or $80000000);
  38.   clWindowFrame = TColor(COLOR_WINDOWFRAME or $80000000);
  39.   clMenuText = TColor(COLOR_MENUTEXT or $80000000);
  40.   clWindowText = TColor(COLOR_WINDOWTEXT or $80000000);
  41.   clCaptionText = TColor(COLOR_CAPTIONTEXT or $80000000);
  42.   clActiveBorder = TColor(COLOR_ACTIVEBORDER or $80000000);
  43.   clInactiveBorder = TColor(COLOR_INACTIVEBORDER or $80000000);
  44.   clAppWorkSpace = TColor(COLOR_APPWORKSPACE or $80000000);
  45.   clHighlight = TColor(COLOR_HIGHLIGHT or $80000000);
  46.   clHighlightText = TColor(COLOR_HIGHLIGHTTEXT or $80000000);
  47.   clBtnFace = TColor(COLOR_BTNFACE or $80000000);
  48.   clBtnShadow = TColor(COLOR_BTNSHADOW or $80000000);
  49.   clGrayText = TColor(COLOR_GRAYTEXT or $80000000);
  50.   clBtnText = TColor(COLOR_BTNTEXT or $80000000);
  51.   clInactiveCaptionText = TColor(COLOR_INACTIVECAPTIONTEXT or $80000000);
  52.   clBtnHighlight = TColor(COLOR_BTNHIGHLIGHT or $80000000);
  53.   cl3DDkShadow = TColor(COLOR_3DDKSHADOW or $80000000);
  54.   cl3DLight = TColor(COLOR_3DLIGHT or $80000000);
  55.   clInfoText = TColor(COLOR_INFOTEXT or $80000000);
  56.   clInfoBk = TColor(COLOR_INFOBK or $80000000);
  57.  
  58.   clBlack = TColor($000000);
  59.   clMaroon = TColor($000080);
  60.   clGreen = TColor($008000);
  61.   clOlive = TColor($008080);
  62.   clNavy = TColor($800000);
  63.   clPurple = TColor($800080);
  64.   clTeal = TColor($808000);
  65.   clGray = TColor($808080);
  66.   clSilver = TColor($C0C0C0);
  67.   clRed = TColor($0000FF);
  68.   clLime = TColor($00FF00);
  69.   clYellow = TColor($00FFFF);
  70.   clBlue = TColor($FF0000);
  71.   clFuchsia = TColor($FF00FF);
  72.   clAqua = TColor($FFFF00);
  73.   clLtGray = TColor($C0C0C0);
  74.   clDkGray = TColor($808080);
  75.   clWhite = TColor($FFFFFF);
  76.   clNone = TColor($1FFFFFFF);
  77.   clDefault = TColor($20000000);
  78.  
  79. const
  80.   cmBlackness = BLACKNESS;
  81.   cmDstInvert = DSTINVERT;
  82.   cmMergeCopy = MERGECOPY;
  83.   cmMergePaint = MERGEPAINT;
  84.   cmNotSrcCopy = NOTSRCCOPY;
  85.   cmNotSrcErase = NOTSRCERASE;
  86.   cmPatCopy = PATCOPY;
  87.   cmPatInvert = PATINVERT;
  88.   cmPatPaint = PATPAINT;
  89.   cmSrcAnd = SRCAND;
  90.   cmSrcCopy = SRCCOPY;
  91.   cmSrcErase = SRCERASE;
  92.   cmSrcInvert = SRCINVERT;
  93.   cmSrcPaint = SRCPAINT;
  94.   cmWhiteness = WHITENESS;
  95.  
  96. type
  97.   {$EXTERNALSYM HMETAFILE}
  98.   HMETAFILE = THandle;
  99.   {$EXTERNALSYM HENHMETAFILE}
  100.   HENHMETAFILE = THandle;
  101.  
  102.   EInvalidGraphic = class(Exception);
  103.   EInvalidGraphicOperation = class(Exception);
  104.  
  105.   TGraphic = class;
  106.   TBitmap = class;
  107.   TIcon = class;
  108.   TMetafile = class;
  109.  
  110.   TResData = record
  111.     Handle: THandle;
  112.   end;
  113.  
  114.   TFontPitch = (fpDefault, fpVariable, fpFixed);
  115.   TFontName = type string;
  116.   TFontCharset = 0..255;
  117.  
  118.   { Changes to the following types should be reflected in the $HPPEMIT directives. }
  119.  
  120.   TFontDataName = string[LF_FACESIZE - 1];
  121.   {$NODEFINE TFontDataName}
  122.   TFontStyle = (fsBold, fsItalic, fsUnderline, fsStrikeOut);
  123.   {$NODEFINE TFontStyle}
  124.   TFontStyles = set of TFontStyle;
  125.   TFontStylesBase = set of TFontStyle;
  126.   {$NODEFINE TFontStylesBase}
  127.  
  128.   (*$HPPEMIT 'namespace Graphics'*)
  129.   (*$HPPEMIT '{'*)
  130.   (*$HPPEMIT '  enum TFontStyle { fsBold, fsItalic, fsUnderline, fsStrikeOut };'*)
  131.   (*$HPPEMIT '  typedef SmallStringBase<31> TFontDataName;'*)
  132.   (*$HPPEMIT '  typedef SetBase<TFontStyle, fsBold, fsStrikeOut> TFontStylesBase;'*)
  133.   (*$HPPEMIT '}'*)
  134.  
  135.   TFontData = record
  136.     Handle: HFont;
  137.     Height: Integer;
  138.     Pitch: TFontPitch;
  139.     Style: TFontStylesBase;
  140.     Charset: TFontCharset;
  141.     Name: TFontDataName;
  142.   end;
  143.  
  144.   TPenStyle = (psSolid, psDash, psDot, psDashDot, psDashDotDot, psClear,
  145.     psInsideFrame);
  146.   TPenMode = (pmBlack, pmWhite, pmNop, pmNot, pmCopy, pmNotCopy,
  147.     pmMergePenNot, pmMaskPenNot, pmMergeNotPen, pmMaskNotPen, pmMerge,
  148.     pmNotMerge, pmMask, pmNotMask, pmXor, pmNotXor);
  149.  
  150.   TPenData = record
  151.     Handle: HPen;
  152.     Color: TColor;
  153.     Width: Integer;
  154.     Style: TPenStyle;
  155.   end;
  156.  
  157.   TBrushStyle = (bsSolid, bsClear, bsHorizontal, bsVertical,
  158.     bsFDiagonal, bsBDiagonal, bsCross, bsDiagCross);
  159.  
  160.   TBrushData = record
  161.     Handle: HBrush;
  162.     Color: TColor;
  163.     Bitmap: TBitmap;
  164.     Style: TBrushStyle;
  165.   end;
  166.  
  167.   PResource = ^TResource;
  168.   TResource = record
  169.     Next: PResource;
  170.     RefCount: Integer;
  171.     Handle: THandle;
  172.     HashCode: Word;
  173.     case Integer of
  174.       0: (Data: TResData);
  175.       1: (Font: TFontData);
  176.       2: (Pen: TPenData);
  177.       3: (Brush: TBrushData);
  178.   end;
  179.  
  180.   TGraphicsObject = class(TPersistent)
  181.   protected
  182.     procedure Changed; dynamic;
  183.     procedure Lock;
  184.     procedure Unlock;
  185.   public
  186.     property OnChange: TNotifyEvent;
  187.     property OwnerCriticalSection: PRTLCriticalSection;
  188.   end;
  189.  
  190.   IChangeNotifier = interface
  191.     ['{1FB62321-44A7-11D0-9E93-0020AF3D82DA}']
  192.     procedure Changed;
  193.   end;
  194.  
  195.   TFont = class(TGraphicsObject)
  196.   protected
  197.     procedure Changed; override;
  198.     function GetHandle: HFont;
  199.     function GetHeight: Integer;
  200.     function GetName: TFontName;
  201.     function GetPitch: TFontPitch;
  202.     function GetSize: Integer;
  203.     function GetStyle: TFontStyles;
  204.     function GetCharset: TFontCharset;
  205.     procedure SetColor(Value: TColor);
  206.     procedure SetHandle(Value: HFont);
  207.     procedure SetHeight(Value: Integer);
  208.     procedure SetName(const Value: TFontName);
  209.     procedure SetPitch(Value: TFontPitch);
  210.     procedure SetSize(Value: Integer);
  211.     procedure SetStyle(Value: TFontStyles);
  212.     procedure SetCharset(Value: TFontCharset);
  213.   public
  214.     constructor Create;
  215.     destructor Destroy; override;
  216.     procedure Assign(Source: TPersistent); override;
  217.     property FontAdapter: IChangeNotifier;
  218.     property Handle: HFont;
  219.     property PixelsPerInch: Integer;
  220.   published
  221.     property Charset: TFontCharset;
  222.     property Color: TColor;
  223.     property Height: Integer;
  224.     property Name: TFontName;
  225.     property Pitch: TFontPitch default fpDefault;
  226.     property Size: Integer;
  227.     property Style: TFontStyles;
  228.   end;
  229.  
  230.   TPen = class(TGraphicsObject)
  231.   protected
  232.     function GetColor: TColor;
  233.     procedure SetColor(Value: TColor);
  234.     function GetHandle: HPen;
  235.     procedure SetHandle(Value: HPen);
  236.     procedure SetMode(Value: TPenMode);
  237.     function GetStyle: TPenStyle;
  238.     procedure SetStyle(Value: TPenStyle);
  239.     function GetWidth: Integer;
  240.     procedure SetWidth(Value: Integer);
  241.   public
  242.     constructor Create;
  243.     destructor Destroy; override;
  244.     procedure Assign(Source: TPersistent); override;
  245.     property Handle: HPen;
  246.   published
  247.     property Color: TColor default clBlack;
  248.     property Mode: TPenMode default pmCopy;
  249.     property Style: TPenStyle default psSolid;
  250.     property Width: Integer default 1;
  251.   end;
  252.  
  253.   TBrush = class(TGraphicsObject)
  254.   protected
  255.     function GetBitmap: TBitmap;
  256.     procedure SetBitmap(Value: TBitmap);
  257.     function GetColor: TColor;
  258.     procedure SetColor(Value: TColor);
  259.     function GetHandle: HBrush;
  260.     procedure SetHandle(Value: HBrush);
  261.     function GetStyle: TBrushStyle;
  262.     procedure SetStyle(Value: TBrushStyle);
  263.   public
  264.     constructor Create;
  265.     destructor Destroy; override;
  266.     procedure Assign(Source: TPersistent); override;
  267.     property Bitmap: TBitmap;
  268.     property Handle: HBrush;
  269.   published
  270.     property Color: TColor default clWhite;
  271.     property Style: TBrushStyle default bsSolid;
  272.   end;
  273.  
  274.   TFillStyle = (fsSurface, fsBorder);
  275.   TFillMode = (fmAlternate, fmWinding);
  276.  
  277.   TCopyMode = Longint;
  278.  
  279.   TCanvasStates = (csHandleValid, csFontValid, csPenValid, csBrushValid);
  280.   TCanvasState = set of TCanvasStates;
  281.  
  282.   TCanvas = class(TPersistent)
  283.   protected
  284.     procedure Changed; virtual;
  285.     procedure Changing; virtual;
  286.     procedure CreateHandle; virtual;
  287.     procedure RequiredState(ReqState: TCanvasState);
  288.   public
  289.     constructor Create;
  290.     destructor Destroy; override;
  291.     procedure Arc(X1, Y1, X2, Y2, X3, Y3, X4, Y4: Integer);
  292.     procedure BrushCopy(const Dest: TRect; Bitmap: TBitmap;
  293.       const Source: TRect; Color: TColor);
  294.     procedure Chord(X1, Y1, X2, Y2, X3, Y3, X4, Y4: Integer);
  295.     procedure CopyRect(const Dest: TRect; Canvas: TCanvas;
  296.       const Source: TRect);
  297.     procedure Draw(X, Y: Integer; Graphic: TGraphic);
  298.     procedure DrawFocusRect(const Rect: TRect);
  299.     procedure Ellipse(X1, Y1, X2, Y2: Integer);
  300.     procedure FillRect(const Rect: TRect);
  301.     procedure FloodFill(X, Y: Integer; Color: TColor; FillStyle: TFillStyle);
  302.     procedure FrameRect(const Rect: TRect);
  303.     procedure LineTo(X, Y: Integer);
  304.     procedure Lock;
  305.     procedure MoveTo(X, Y: Integer);
  306.     procedure Pie(X1, Y1, X2, Y2, X3, Y3, X4, Y4: Integer);
  307.     procedure Polygon(const Points: array of TPoint);
  308.     procedure Polyline(const Points: array of TPoint);
  309.     procedure PolyBezier(const Points: array of TPoint);
  310.     procedure PolyBezierTo(const Points: array of TPoint);
  311.     procedure Rectangle(X1, Y1, X2, Y2: Integer);
  312.     procedure Refresh;
  313.     procedure RoundRect(X1, Y1, X2, Y2, X3, Y3: Integer);
  314.     procedure StretchDraw(const Rect: TRect; Graphic: TGraphic);
  315.     function TextExtent(const Text: string): TSize;
  316.     function TextHeight(const Text: string): Integer;
  317.     procedure TextOut(X, Y: Integer; const Text: string);
  318.     procedure TextRect(Rect: TRect; X, Y: Integer; const Text: string);
  319.     function TextWidth(const Text: string): Integer;
  320.     function TryLock: Boolean;
  321.     procedure Unlock;
  322.     property ClipRect: TRect;
  323.     property Handle: HDC;
  324.     property LockCount: Integer;
  325.     property PenPos: TPoint;
  326.     property Pixels[X, Y: Integer]: TColor;
  327.     property OnChange: TNotifyEvent;
  328.     property OnChanging: TNotifyEvent;
  329.   published
  330.     property Brush: TBrush;
  331.     property CopyMode: TCopyMode default cmSrcCopy;
  332.     property Font: TFont;
  333.     property Pen: TPen;
  334.   end;
  335.  
  336.   { TProgressEvent is a generic progress notification event which may be
  337.         used by TGraphic classes with computationally intensive (slow)
  338.         operations, such as loading, storing, or transforming image data.
  339.     Event params:
  340.       Stage - Indicates whether this call to the OnProgress event is to
  341.         prepare for, process, or clean up after a graphic operation.  If
  342.         OnProgress is called at all, the first call for a graphic operation
  343.         will be with Stage = psStarting, to allow the OnProgress event handler
  344.         to allocate whatever resources it needs to process subsequent progress
  345.         notifications.  After Stage = psStarting, you are guaranteed that
  346.         OnProgress will be called again with Stage = psEnding to allow you
  347.         to free those resources, even if the graphic operation is aborted by
  348.         an exception.  Zero or more calls to OnProgress with Stage = psRunning
  349.         may occur between the psStarting and psEnding calls.
  350.       PercentDone - The ratio of work done to work remaining, on a scale of
  351.         0 to 100.  Values may repeat or even regress (get smaller) in
  352.         successive calls.  PercentDone is usually only a guess, and the
  353.         guess may be dramatically altered as new information is discovered
  354.         in decoding the image.
  355.       RedrawNow - Indicates whether the graphic can be/should be redrawn
  356.         immediately.  Useful for showing successive approximations of
  357.         an image as data is available instead of waiting for all the data
  358.         to arrive before drawing anything.  Since there is no message loop
  359.         activity during graphic operations, you should call Update to force
  360.         a control to be redrawn immediately in the OnProgress event handler.
  361.         Redrawing a graphic when RedrawNow = False could corrupt the image
  362.         and/or cause exceptions.
  363.       Rect - Area of image that has changed and needs to be redrawn.
  364.       Msg - Optional text describing in one or two words what the graphic
  365.         class is currently working on.  Ex:  "Loading" "Storing"
  366.         "Reducing colors".  The Msg string can also be empty.
  367.         Msg strings should be resourced for translation,  should not
  368.         contain trailing periods, and should be used only for
  369.         display purposes.  (do not: if Msg = 'Loading' then...)
  370.   }
  371.  
  372.   TProgressStage = (psStarting, psRunning, psEnding);
  373.   TProgressEvent = procedure (Sender: TObject; Stage: TProgressStage;
  374.     PercentDone: Byte; RedrawNow: Boolean; const R: TRect; const Msg: string) of object;
  375.  
  376.   { The TGraphic class is a abstract base class for dealing with graphic images
  377.     such as metafile, bitmaps, icons, and other image formats.
  378.       LoadFromFile - Read the graphic from the file system.  The old contents of
  379.         the graphic are lost.  If the file is not of the right format, an
  380.         exception will be generated.
  381.       SaveToFile - Writes the graphic to disk in the file provided.
  382.       LoadFromStream - Like LoadFromFile except source is a stream (e.g.
  383.         TBlobStream).
  384.       SaveToStream - stream analogue of SaveToFile.
  385.       LoadFromClipboardFormat - Replaces the current image with the data
  386.         provided.  If the TGraphic does not support that format it will generate
  387.         an exception.
  388.       SaveToClipboardFormats - Converts the image to a clipboard format.  If the
  389.         image does not support being translated into a clipboard format it
  390.         will generate an exception.
  391.       Height - The native, unstretched, height of the graphic.
  392.       Palette - Color palette of image.  Zero if graphic doesn't need/use palettes.
  393.       Transparent - Image does not completely cover its rectangular area
  394.       Width - The native, unstretched, width of the graphic.
  395.       OnChange - Called whenever the graphic changes
  396.       PaletteModified - Indicates in OnChange whether color palette has changed.
  397.         Stays true until whoever's responsible for realizing this new palette
  398.         (ex: TImage) sets it to False.
  399.       OnProgress - Generic progress indicator event. Propagates out to TPicture
  400.         and TImage OnProgress events.}
  401.  
  402.   TGraphic = class(TPersistent)
  403.   protected
  404.     constructor Create; virtual;
  405.     procedure Changed(Sender: TObject); virtual;
  406.     procedure DefineProperties(Filer: TFiler); override;
  407.     procedure Draw(ACanvas: TCanvas; const Rect: TRect); virtual; abstract;
  408.     function Equals(Graphic: TGraphic): Boolean; virtual;
  409.     function GetEmpty: Boolean; virtual; abstract;
  410.     function GetHeight: Integer; virtual; abstract;
  411.     function GetPalette: HPALETTE; virtual;
  412.     function GetTransparent: Boolean; virtual;
  413.     function GetWidth: Integer; virtual; abstract;
  414.     procedure Progress(Sender: TObject; Stage: TProgressStage;
  415.       PercentDone: Byte;  RedrawNow: Boolean; const R: TRect; const Msg: string); dynamic;
  416.     procedure ReadData(Stream: TStream); virtual;
  417.     procedure SetHeight(Value: Integer); virtual; abstract;
  418.     procedure SetPalette(Value: HPALETTE); virtual;
  419.     procedure SetTransparent(Value: Boolean); virtual;
  420.     procedure SetWidth(Value: Integer); virtual; abstract;
  421.     procedure WriteData(Stream: TStream); virtual;
  422.   public
  423.     procedure LoadFromFile(const Filename: string); virtual;
  424.     procedure SaveToFile(const Filename: string); virtual;
  425.     procedure LoadFromStream(Stream: TStream); virtual; abstract;
  426.     procedure SaveToStream(Stream: TStream); virtual; abstract;
  427.     procedure LoadFromClipboardFormat(AFormat: Word; AData: THandle;
  428.       APalette: HPALETTE); virtual; abstract;
  429.     procedure SaveToClipboardFormat(var AFormat: Word; var AData: THandle;
  430.       var APalette: HPALETTE); virtual; abstract;
  431.     property Empty: Boolean;
  432.     property Height: Integer;
  433.     property Modified: Boolean;
  434.     property Palette: HPALETTE;
  435.     property PaletteModified: Boolean;
  436.     property Transparent: Boolean;
  437.     property Width: Integer;
  438.     property OnChange: TNotifyEvent;
  439.     property OnProgress: TProgressEvent;
  440.   end;
  441.  
  442.   TGraphicClass = class of TGraphic;
  443.  
  444.   { TPicture }
  445.   { TPicture is a TGraphic container.  It is used in place of a TGraphic if the
  446.     graphic can be of any TGraphic class.  LoadFromFile and SaveToFile are
  447.     polymorphic. For example, if the TPicture is holding an Icon, you can
  448.     LoadFromFile a bitmap file, where if the class was TIcon you could only read
  449.     .ICO files.
  450.       LoadFromFile - Reads a picture from disk.  The TGraphic class created
  451.         determined by the file extension of the file.  If the file extension is
  452.         not recognized an exception is generated.
  453.       SaveToFile - Writes the picture to disk.
  454.       LoadFromClipboardFormat - Reads the picture from the handle provided in
  455.         the given clipboard format.  If the format is not supported, an
  456.         exception is generated.
  457.       SaveToClipboardFormats - Allocates a global handle and writes the picture
  458.         in its native clipboard format (CF_BITMAP for bitmaps, CF_METAFILE
  459.         for metafiles, etc.).  Formats will contain the formats written.
  460.         Returns the number of clipboard items written to the array pointed to
  461.         by Formats and Datas or would be written if either Formats or Datas are
  462.         nil.
  463.       SupportsClipboardFormat - Returns true if the given clipboard format
  464.         is supported by LoadFromClipboardFormat.
  465.       Assign - Copys the contents of the given TPicture.  Used most often in
  466.         the implementation of TPicture properties.
  467.       RegisterFileFormat - Register a new TGraphic class for use in
  468.         LoadFromFile.
  469.       RegisterClipboardFormat - Registers a new TGraphic class for use in
  470.         LoadFromClipboardFormat.
  471.       UnRegisterGraphicClass - Removes all references to the specified TGraphic
  472.         class and all its descendents from the file format and clipboard format
  473.         internal lists.
  474.       Height - The native, unstretched, height of the picture.
  475.       Width - The native, unstretched, width of the picture.
  476.       Graphic - The TGraphic object contained by the TPicture
  477.       Bitmap - Returns a bitmap.  If the contents is not already a bitmap, the
  478.         contents are thrown away and a blank bitmap is returned.
  479.       Icon - Returns an icon.  If the contents is not already an icon, the
  480.         contents are thrown away and a blank icon is returned.
  481.       Metafile - Returns a metafile.  If the contents is not already a metafile,
  482.         the contents are thrown away and a blank metafile is returned. }
  483.   TPicture = class(TPersistent)
  484.   protected
  485.     procedure AssignTo(Dest: TPersistent); override;
  486.     procedure Changed(Sender: TObject); dynamic;
  487.     procedure Progress(Sender: TObject; Stage: TProgressStage;
  488.       PercentDone: Byte;  RedrawNow: Boolean; const R: TRect; const Msg: string); dynamic;
  489.     procedure DefineProperties(Filer: TFiler); override;
  490.   public
  491.     constructor Create;
  492.     destructor Destroy; override;
  493.     procedure LoadFromFile(const Filename: string);
  494.     procedure SaveToFile(const Filename: string);
  495.     procedure LoadFromClipboardFormat(AFormat: Word; AData: THandle;
  496.       APalette: HPALETTE);
  497.     procedure SaveToClipboardFormat(var AFormat: Word; var AData: THandle;
  498.       var APalette: HPALETTE);
  499.     class function SupportsClipboardFormat(AFormat: Word): Boolean;
  500.     procedure Assign(Source: TPersistent); override;
  501.     class procedure RegisterFileFormat(const AExtension, ADescription: string;
  502.       AGraphicClass: TGraphicClass);
  503.     class procedure RegisterFileFormatRes(const AExtension: String;
  504.       ADescriptionResID: Integer; AGraphicClass: TGraphicClass);
  505.     class procedure RegisterClipboardFormat(AFormat: Word;
  506.       AGraphicClass: TGraphicClass);
  507.     class procedure UnregisterGraphicClass(AClass: TGraphicClass);
  508.     property Bitmap: TBitmap;
  509.     property Graphic: TGraphic;
  510.     property PictureAdapter: IChangeNotifier;
  511.     property Height: Integer;
  512.     property Icon: TIcon;
  513.     property Metafile: TMetafile;
  514.     property Width: Integer;
  515.     property OnChange: TNotifyEvent;
  516.     property OnProgress: TProgressEvent;
  517.   end;
  518.  
  519.   { TMetafile }
  520.   { TMetafile is an encapsulation of the Win32 Enhanced metafile.
  521.       Handle - The metafile handle.
  522.       Enhanced - determines how the metafile will be stored on disk.
  523.         Enhanced = True (default) stores as EMF (Win32 Enhanced Metafile),
  524.         Enhanced = False stores as WMF (Windows 3.1 Metafile, with Aldus header).
  525.         The in-memory format is always EMF.  WMF has very limited capabilities;
  526.         storing as WMF will lose information that would be retained by EMF.
  527.         This property is set to match the metafile type when loaded from a
  528.         stream or file.  This maintains form file compatibility with 16 bit
  529.         Delphi (If loaded as WMF, then save as WMF).
  530.       Inch - The units per inch assumed by a WMF metafile.  Used to alter
  531.         scale when writing as WMF, but otherwise this property is obsolete.
  532.         Enhanced metafiles maintain complete scale information internally.
  533.       MMWidth,
  534.       MMHeight: Width and Height in 0.01 millimeter units, the native
  535.         scale used by enhanced metafiles.  The Width and Height properties
  536.         are always in screen device pixel units; you can avoid loss of
  537.         precision in converting between device pixels and mm by setting
  538.         or reading the dimentions in mm with these two properties.
  539.       CreatedBy - Optional name of the author or application used to create
  540.         the metafile.
  541.       Description - Optional text description of the metafile.
  542.       You can set the CreatedBy and Description of a new metafile by calling
  543.       TMetafileCanvas.CreateWithComment.
  544.  
  545.     TMetafileCanvas
  546.       To create a metafile image from scratch, you must draw the image in
  547.       a metafile canvas.  When the canvas is destroyed, it transfers the
  548.       image into the metafile object provided to the canvas constructor.
  549.       After the image is drawn on the canvas and the canvas is destroyed,
  550.       the image is 'playable' in the metafile object.  Like this:
  551.  
  552.       MyMetafile := TMetafile.Create;
  553.       MyMetafile.Width := 200;
  554.       MyMetafile.Height := 200;
  555.       with TMetafileCanvas.Create(MyMetafile, 0) do
  556.       try
  557.         Brush.Color := clRed;
  558.         Ellipse(0,0,100,100);
  559.         ...
  560.       finally
  561.         Free;
  562.       end;
  563.       Form1.Canvas.Draw(0,0,MyMetafile);  (* 1 red circle  *)
  564.  
  565.       To add to an existing metafile image, create a metafile canvas
  566.       and play the source metafile into the metafile canvas.  Like this:
  567.  
  568.       (* continued from previous example, so MyMetafile contains an image *)
  569.       with TMetafileCanvas.Create(MyMetafile, 0) do
  570.       try
  571.         Draw(0,0,MyMetafile);
  572.         Brush.Color := clBlue;
  573.         Ellipse(100,100,200,200);
  574.         ...
  575.       finally
  576.         Free;
  577.       end;
  578.       Form1.Canvas.Draw(0,0,MyMetafile);  (* 1 red circle and 1 blue circle *)
  579.   }
  580.  
  581.   TMetafileCanvas = class(TCanvas)
  582.   public
  583.     constructor Create(AMetafile: TMetafile; ReferenceDevice: HDC);
  584.     constructor CreateWithComment(AMetafile: TMetafile; ReferenceDevice: HDC;
  585.       const CreatedBy, Description: String);
  586.     destructor Destroy; override;
  587.   end;
  588.  
  589.   TSharedImage = class
  590.   protected
  591.     procedure Reference;
  592.     procedure Release;
  593.     procedure FreeHandle; virtual; abstract;
  594.     property RefCount: Integer;
  595.   end;
  596.  
  597.   TMetafileImage = class(TSharedImage)
  598.   protected
  599.     procedure FreeHandle; override;
  600.   public
  601.     destructor Destroy; override;
  602.   end;
  603.  
  604.   TMetafile = class(TGraphic)
  605.   protected
  606.     function GetEmpty: Boolean; override;
  607.     function GetHeight: Integer; override;
  608.     function GetPalette: HPALETTE; override;
  609.     function GetWidth: Integer; override;
  610.     procedure Draw(ACanvas: TCanvas; const Rect: TRect); override;
  611.     procedure ReadData(Stream: TStream); override;
  612.     procedure ReadEMFStream(Stream: TStream);
  613.     procedure ReadWMFStream(Stream: TStream; Length: Longint);
  614.     procedure SetHeight(Value: Integer); override;
  615.     procedure SetTransparent(Value: Boolean); override;
  616.     procedure SetWidth(Value: Integer); override;
  617.     function  TestEMF(Stream: TStream): Boolean;
  618.     procedure WriteData(Stream: TStream); override;
  619.     procedure WriteEMFStream(Stream: TStream);
  620.     procedure WriteWMFStream(Stream: TStream);
  621.   public
  622.     constructor Create; override;
  623.     destructor Destroy; override;
  624.     procedure Clear;
  625.     procedure LoadFromStream(Stream: TStream); override;
  626.     procedure SaveToFile(const Filename: String); override;
  627.     procedure SaveToStream(Stream: TStream); override;
  628.     procedure LoadFromClipboardFormat(AFormat: Word; AData: THandle;
  629.       APalette: HPALETTE); override;
  630.     procedure SaveToClipboardFormat(var AFormat: Word; var AData: THandle;
  631.       var APalette: HPALETTE); override;
  632.     procedure Assign(Source: TPersistent); override;
  633.     function ReleaseHandle: HENHMETAFILE;
  634.     property CreatedBy: String;
  635.     property Description: String;
  636.     property Enhanced: Boolean default True;
  637.     property Handle: HENHMETAFILE;
  638.     property MMWidth: Integer;
  639.     property MMHeight: Integer;
  640.     property Inch: Word;
  641.   end;
  642.  
  643.   { TBitmap }
  644.   { TBitmap is an encapsulation of a Windows HBITMAP and HPALETTE.  It manages
  645.     the palette realizing automatically as well as having a Canvas to allow
  646.     modifications to the image.  Creating copies of a TBitmap is very fast
  647.     since the handle is copied not the image.  If the image is modified, and
  648.     the handle is shared by more than one TBitmap object, the image is copied
  649.     before the modification is performed (i.e. copy on write).
  650.       Canvas - Allows drawing on the bitmap.
  651.       Handle - The HBITMAP encapsulated by the TBitmap.  Grabbing the handle
  652.         directly should be avoided since it causes the HBITMAP to be copied if
  653.         more than one TBitmap share the handle.
  654.       Palette - The HPALETTE realized by the TBitmap.  Grabbing this handle
  655.         directly should be avoided since it causes the HPALETTE to be copied if
  656.         more than one TBitmap share the handle.
  657.       Monochrome - True if the bitmap is a monochrome bitmap }
  658.  
  659.   TBitmapImage = class(TSharedImage)
  660.   protected
  661.     destructor Destroy; override;
  662.     procedure FreeHandle; override;
  663.   end;
  664.  
  665.   TBitmapHandleType = (bmDIB, bmDDB);
  666.   TPixelFormat = (pfDevice, pf1bit, pf4bit, pf8bit, pf15bit, pf16bit, pf24bit, pf32bit, pfCustom);
  667.   TTransparentMode = (tmAuto, tmFixed);
  668.  
  669.   TBitmap = class(TGraphic)
  670.   protected
  671.     procedure Changed(Sender: TObject); override;
  672.     procedure Draw(ACanvas: TCanvas; const Rect: TRect); override;
  673.     function GetEmpty: Boolean; override;
  674.     function GetHeight: Integer; override;
  675.     function GetPalette: HPALETTE; override;
  676.     function GetWidth: Integer; override;
  677.     procedure HandleNeeded;
  678.     procedure MaskHandleNeeded;
  679.     procedure PaletteNeeded;
  680.     procedure ReadData(Stream: TStream); override;
  681.     procedure SetHeight(Value: Integer); override;
  682.     procedure SetPalette(Value: HPALETTE); override;
  683.     procedure SetWidth(Value: Integer); override;
  684.     procedure WriteData(Stream: TStream); override;
  685.   public
  686.     constructor Create; override;
  687.     destructor Destroy; override;
  688.     procedure Assign(Source: TPersistent); override;
  689.     procedure Dormant;
  690.     procedure FreeImage;
  691.     procedure LoadFromClipboardFormat(AFormat: Word; AData: THandle;
  692.       APalette: HPALETTE); override;
  693.     procedure LoadFromStream(Stream: TStream); override;
  694.     procedure LoadFromResourceName(Instance: THandle; const ResName: String);
  695.     procedure LoadFromResourceID(Instance: THandle; ResID: Integer);
  696.     procedure Mask(TransparentColor: TColor);
  697.     function ReleaseHandle: HBITMAP;
  698.     function ReleaseMaskHandle: HBITMAP;
  699.     function ReleasePalette: HPALETTE;
  700.     procedure SaveToClipboardFormat(var Format: Word; var Data: THandle;
  701.       var APalette: HPALETTE); override;
  702.     procedure SaveToStream(Stream: TStream); override;
  703.     property Canvas: TCanvas;
  704.     property Handle: HBITMAP;
  705.     property HandleType: TBitmapHandleType;
  706.     property IgnorePalette: Boolean;
  707.     property MaskHandle: HBITMAP;
  708.     property Monochrome: Boolean;
  709.     property PixelFormat: TPixelFormat;
  710.     property ScanLine[Row: Integer]: Pointer;
  711.     property TransparentColor: TColor;
  712.     property TransparentMode: TTransparentMode default tmAuto;
  713.   end;
  714.  
  715.   { TIcon }
  716.   { TIcon encapsulates window HICON handle. Drawing of an icon does not stretch
  717.     so calling stretch draw is not meaningful.
  718.       Handle - The HICON used by the TIcon. }
  719.  
  720.   TIconImage = class(TSharedImage)
  721.   protected
  722.     destructor Destroy; override;
  723.     procedure FreeHandle; override;
  724.   end;
  725.  
  726.   TIcon = class(TGraphic)
  727.   protected
  728.     procedure Draw(ACanvas: TCanvas; const Rect: TRect); override;
  729.     function GetEmpty: Boolean; override;
  730.     function GetHeight: Integer; override;
  731.     function GetWidth: Integer; override;
  732.     procedure SetHeight(Value: Integer); override;
  733.     procedure SetTransparent(Value: Boolean); override;
  734.     procedure SetWidth(Value: Integer); override;
  735.     procedure LoadFromClipboardFormat(AFormat: Word; AData: THandle;
  736.       APalette: HPALETTE); override;
  737.     procedure SaveToClipboardFormat(var Format: Word; var Data: THandle;
  738.       var APalette: HPALETTE); override;
  739.   public
  740.     constructor Create; override;
  741.     destructor Destroy; override;
  742.     procedure Assign(Source: TPersistent); override;
  743.     procedure LoadFromStream(Stream: TStream); override;
  744.     function ReleaseHandle: HICON;
  745.     procedure SaveToStream(Stream: TStream); override;
  746.     property Handle: HICON;
  747.   end;
  748.  
  749. var    // New TFont instances are intialized with the values in this structure:
  750.   DefFontData: TFontData = (
  751.     Handle: 0;
  752.     Height: 0;
  753.     Pitch: fpDefault;
  754.     Style: [];
  755.     Charset : DEFAULT_CHARSET;
  756.     Name: 'MS Sans Serif');
  757.  
  758.  
  759. var
  760.   SystemPalette16: HPalette; // 16 color palette that maps to the system palette
  761.  
  762. var
  763.   DDBsOnly: Boolean = False; // True = Load all BMPs as device bitmaps.
  764.                              // Not recommended.
  765.  
  766. function GraphicFilter(GraphicClass: TGraphicClass): string;
  767. function GraphicExtension(GraphicClass: TGraphicClass): string;
  768. function GraphicFileMask(GraphicClass: TGraphicClass): string;
  769.  
  770. function ColorToRGB(Color: TColor): Longint;
  771. function ColorToString(Color: TColor): string;
  772. function StringToColor(const S: string): TColor;
  773. procedure GetColorValues(Proc: TGetStrProc);
  774. function ColorToIdent(Color: Longint; var Ident: string): Boolean;
  775. function IdentToColor(const Ident: string; var Color: Longint): Boolean;
  776. procedure GetCharsetValues(Proc: TGetStrProc);
  777. function CharsetToIdent(Charset: Longint; var Ident: string): Boolean;
  778. function IdentToCharset(const Ident: string; var Charset: Longint): Boolean;
  779.  
  780. procedure GetDIBSizes(Bitmap: HBITMAP; var InfoHeaderSize: Integer;
  781.   var ImageSize: DWORD);
  782. function GetDIB(Bitmap: HBITMAP; Palette: HPALETTE; var BitmapInfo; var Bits): Boolean;
  783.  
  784. function CopyPalette(Palette: HPALETTE): HPALETTE;
  785.  
  786. procedure PaletteChanged;
  787. procedure FreeMemoryContexts;
  788.  
  789. function GetDefFontCharSet: TFontCharSet;
  790.  
  791. function TransparentStretchBlt(DstDC: HDC; DstX, DstY, DstW, DstH: Integer;
  792.   SrcDC: HDC; SrcX, SrcY, SrcW, SrcH: Integer; MaskDC: HDC; MaskX,
  793.   MaskY: Integer): Boolean;
  794.  
  795. function CreateMappedBmp(Handle: HBITMAP; const OldColors, NewColors: array of TColor): HBITMAP;
  796. function CreateMappedRes(Instance: THandle; ResName: PChar; const OldColors, NewColors: array of TColor): HBITMAP;
  797. function CreateGrayMappedBmp(Handle: HBITMAP): HBITMAP;
  798. function CreateGrayMappedRes(Instance: THandle; ResName: PChar): HBITMAP;
  799.  
  800. implementation
  801.